package se.teamscape.shared.model;

import java.util.ArrayList;
import java.util.Date;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import se.teamscape.client.controller.AppController;
import se.teamscape.client.utils.NameValuePair;

/**
 * Model/entity representation of a person/user
 * 
 * @author Andreas Borglin
 * 
 */
@SuppressWarnings("serial")
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Person implements Model {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String encodedKey;

    @Persistent
    private String firstName;

    @Persistent
    private String lastName;

    @Persistent
    private String email;

    @Persistent
    private String phone;

    @Persistent
    private Date birthday;

    @Persistent
    private String password;

    @Persistent
    private Integer adminLevel;

    public Person() {

    }

    @Override
    public String toString() {
        return getDisplayName();
    }

    /*
     * Return this class data as name/value string pairs.
     */
    public static ArrayList<NameValuePair<String>> getAsList(Person p) {

        ArrayList<NameValuePair<String>> list = new ArrayList<NameValuePair<String>>();

        list
                .add(new NameValuePair<String>(AppController.getConstants().firstName(), p.firstName == null ? ""
                        : p.firstName));

        list.add(new NameValuePair<String>(AppController.getConstants().lastName(), p.lastName == null ? "" : p.lastName));

        list.add(new NameValuePair<String>(AppController.getConstants().email(), p.email == null ? "" : p.email));

        list.add(new NameValuePair<String>(AppController.getConstants().phone(), p.phone == null ? "" : p.phone));

        return list;
    }

    @Override
    public String getId() {
        return encodedKey;
    }

    @Override
    public String getDisplayName() {
        return firstName + " " + lastName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPhone() {
        return phone;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setAdminLevel(Integer adminLevel) {
        this.adminLevel = adminLevel;
    }

    public Integer getAdminLevel() {
        return adminLevel;
    }

}
